﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DJ.Dialog;
using DJ.UserControls;
using DJ.ZEF.ThirdParty;
using NSJL.Biz.Background.Client;

namespace DJ.Pages
{
    /// <summary>
    /// BoxPage.xaml 的交互逻辑
    /// </summary>
    public partial class BoxPage : Page
    {
        public BoxPage()
        {
            InitializeComponent();
            Init();
        }
        public void Init()
        {
            var biz = new CommBiz();
            WrapPanel1.Children.Clear();
            var list = biz.GetBoxListWithUserId();
            foreach (var item in list)
            {
                BoxControl btn = new BoxControl();
                btn.Width = 154;
                btn.Height = 125;
                btn.Margin = new Thickness(5, 5, 5, 5);
                btn.Label1.Content = item.BoxNum;
                btn.BoxNum = item.BoxNum;

                if (item.IsLock == true)
                {
                    btn.Label3.Content = "已锁定";
                    btn.Label3.Foreground = (Brush)new BrushConverter().ConvertFromString("#ff0000");
                }
                else
                {
                    btn.Label3.Content = "未锁定";
                }
                btn.Label5.Content =item.IsFree == true?"空闲":"存物";
                string code = null;
                if (!string.IsNullOrEmpty(item.CardCode))
                {
                   code = "编码：" + item.CardCode;
                }
                if (!string.IsNullOrEmpty(item.QrCode))
                {
                   code = "编码：" + item.QrCode;
                }
                btn.Label4.Content = code;

                btn.Label6.Content = item.BoxType ?? "小";

                btn.MouseDown += MouseButtonEventHandler;
                WrapPanel1.Children.Add(btn);
            }
        }
        public void MouseButtonEventHandler(object sender, MouseButtonEventArgs e)
        {
            var u = (BoxControl)sender;
            if (u.IsCheck)
            {
                u.IsCheck = false;
                
                var filepath = "pack://application:,,,/Image/a未选中.png";
                
                u.Grid1.Background = new ImageBrush(new BitmapImage(new Uri(filepath)));
                u.Label1.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                
                u.Label3.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");

                u.Label4.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                u.Label5.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                u.Label6.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
            }
            else
            {
                u.IsCheck = true;

                var filepath = "pack://application:,,,/Image/a选中.png";

                u.Grid1.Background = new ImageBrush(new BitmapImage(new Uri(filepath)));
                u.Label1.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
              
                u.Label3.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");

                u.Label4.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
                u.Label5.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
                u.Label6.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
            }
        }
        //开箱
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var list = GetSelectBox();
            if (list.Count > 0)
            {
                string str = string.Join(",", list.ToArray());
                Task.Run(() =>
                {
                    foreach (var item in list)
                    {
                        LockManager.GetInstance().OpenBox(item);
                        Thread.Sleep(600);
                    }
                });
                var biz=new CommBiz();

                var tip = str + "号箱门开箱成功";
                VoiceHelper.GetInstance().Start(tip);
                biz.EditAdminLogInfo(tip, Main.UserName);
                MessageDialog.ShowDialog(str + "开箱成功");
            }
        }
        //全开
        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            MessageDialog mes=new MessageDialog();
            mes.TextBlock1.Text = "是否确定操作";
            if (mes.ShowDialog() != true)
            {
                return;
            }


            var biz = new CommBiz();
            Task.Run(() =>
            {
                var list = biz.GetBoxList();
                foreach (var item in list)
                {
                    LockManager.GetInstance().OpenBox(item.BoxNum);
                    Thread.Sleep(600);
                }
            });

            var tip = "全开箱门操作成功";
            VoiceHelper.GetInstance().Start(tip);

            biz.EditAdminLogInfo(tip, Main.UserName);
            MessageDialog.ShowDialog("全开箱门操作成功");
        }
        //清箱
        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            var biz = new CommBiz();
            var list = GetSelectBox();
            if (list.Count > 0)
            {
                foreach (BoxControl item in WrapPanel1.Children)
                {
                    if (list.Contains(item.BoxNum))
                    {
                        item.Label5.Content = "空闲";
                        item.Label4.Content = "";
                        item.Label3.Content = "未锁定";
                    }
                }

                string str = string.Join(",", list.ToArray());
                biz.ClearBoxList(list);

                var tip = str + "号箱门清箱成功";
                VoiceHelper.GetInstance().Start(tip);

                biz.EditAdminLogInfo(tip, Main.UserName);
                MessageDialog.ShowDialog(str + "清箱成功");
            }
        }
        //全清
        private void Button_Click3(object sender, RoutedEventArgs e)
        {
            MessageDialog mes = new MessageDialog();
            mes.TextBlock1.Text = "是否确定操作";
            if (mes.ShowDialog() != true)
            {
                return;
            }

            foreach (BoxControl item in WrapPanel1.Children)
            {
                item.Label5.Content = "空闲";
                item.Label4.Content = "";
                item.Label3.Content = "未锁定";
            }

            var biz = new CommBiz();
            biz.AllClearBoxinfo();

            var tip ="全部清箱成功";
            VoiceHelper.GetInstance().Start(tip);

            biz.EditAdminLogInfo(tip, Main.UserName);
            MessageDialog.ShowDialog("全部清箱成功");
        }
        //锁箱
        private void Button_Click4(object sender, RoutedEventArgs e)
        {
            var biz = new CommBiz();
            var list = GetSelectBox();
            if (list.Count > 0)
            {
                string str = string.Join(",", list.ToArray());
                biz.LockBoxList(list);

                var tip = str + "号箱门锁箱成功";
                VoiceHelper.GetInstance().Start(tip);

                biz.EditAdminLogInfo(tip, Main.UserName);
                MessageDialog.ShowDialog(str + "锁箱成功");

                Init();
            }
        }
        //解锁
        private void Button_Click5(object sender, RoutedEventArgs e)
        {
            var biz = new CommBiz();
            var list = GetSelectBox();
            if (list.Count > 0)
            {
                string str = string.Join(",", list.ToArray());
                biz.UnLockBoxList(list);

                var tip = str + "号箱门解箱成功";
                VoiceHelper.GetInstance().Start(tip);

                biz.EditAdminLogInfo(tip, Main.UserName);
                MessageDialog.ShowDialog(str + "解箱成功");

                Init();
            }
        }
        private List<int> GetSelectBox()
        {
            List<int> list = new List<int>();
            foreach (BoxControl item in WrapPanel1.Children)
            {
                if (item.IsCheck)
                {
                    list.Add(item.BoxNum);
                }
            }
            return list;
        }

        private void Page_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Init();
        }
        //全选
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Button click = (Button) sender;
            var name = (string)click.Content;


            foreach (BoxControl item in WrapPanel1.Children)
            {
                var u = item;
                if (name!="全选")
                {
                    click.Content = "全选";
                    
                    u.IsCheck = false;

                    var filepath = "pack://application:,,,/Image/a未选中.png";

                    u.Grid1.Background = new ImageBrush(new BitmapImage(new Uri(filepath)));
                    u.Label1.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                   
                    u.Label3.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");

                    u.Label4.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                    u.Label5.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                    u.Label6.Foreground = (Brush)new BrushConverter().ConvertFromString("#1A1A1A");
                }
                else
                {
                    click.Content = "不选";

                    u.IsCheck = true;

                    var filepath = "pack://application:,,,/Image/a选中.png";

                    u.Grid1.Background = new ImageBrush(new BitmapImage(new Uri(filepath)));
                    u.Label1.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
                  
                    u.Label3.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");

                    u.Label4.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
                    u.Label5.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
                    u.Label6.Foreground = (Brush)new BrushConverter().ConvertFromString("#3CAAE5");
                }
            }
        }
        //修改类型
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var temp = (ComboBoxItem)ComboBox0.SelectedItem;
            var type = (string)temp.Content;
            if (type == "类型")
            {
                MessageDialog.ShowDialog("请选择类型");
                return;
            }

            var biz = new CommBiz();
            var list = GetSelectBox();
            if (list.Count > 0)
            {
                string str = string.Join(",", list.ToArray());
                biz.SetBoxTypeList(list,type);

                biz.EditAdminLogInfo(str + "设置成功", Main.UserName);
                MessageDialog.ShowDialog(str + "设置成功");

                Init();
            }
        }
        private void SCManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
        {
            e.Handled = true;
        }

     
    }
}
